<?php 
namespace app\api\controller\v1;

use think\Db;

//用户类
class UserController extends UserBaseController{


	function activestatus(){
		$data['status'] =0;
		$data['expire'] = 0;
		if($this->user['active_expire']>time()){
			$data['status'] =1;
			$data['expire'] = $this->user['active_expire'];
		}
		return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS'],'data'=>$data]);
	}


	//换取用户资料
	function getinfo(){
		$info = $this->user;
		$has_paypwd = strlen($info['paypwd'])>0 ? 1:0;
		$info = $this->deal_user($info);
		$info['has_paypwd'] = $has_paypwd;
		return json(['error'=>0,'data'=>$info]);
	}

	//重置支付密码
	function uppaypwd(){
		$data= input('param.');
		if(empty($data['paypwd'])){
			return json(['error'=>1,'msg'=>$this->lang['PAYPWD_INPUT_ALARM']]);
		}
		try {
			if($data['old_paypwd']){
				if(trim($data['old_paypwd'])!= $this->user['paypwd']){
					return json(['error'=>1,'msg'=>$this->lang['PAYPWD_ERROR']]);
				}
			}
			Db::name('user')->where('id',$this->user['id'])->update(['paypwd'=>$data['paypwd'],'has_paypwd'=>1]);
			return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS']]);
		} catch (Exception $e) {
			return json(['error'=>1,'msg'=>$this->lang['OPERATE'].$this->lang['FAIL']]);
		}

	}



	//修改头像
	public function upavatar()
    {
    	if($this->request->file('avatar')){
            $file = $this->request->file('avatar');
        }else{
            return '请选择上传文件';
        }
        $custom_path = date('Y/m/d'); // 应用定义的上传目录规则：'Y/md/H/'
        $save_path   = CMF_ROOT .'/public/upload/'. $custom_path;
        $result = $file->validate(['ext'  => 'jpg,jpeg,png','size' => 1024 * 1024])->rule('uniqid')->move($save_path);
        if ($result) {
        	$filename = 'upload/'.$custom_path.'/'.$result->getSaveName();
            $url =  request()->domain().'/'.$filename;
            Db::name('user')->where('id',$this->user['id'])->update(['avatar'=>$filename]);
            return json(['error'=>0,'msg'=>$this->lang['UPLOAD'].$this->lang['SUCCESS'],'data'=>['url'=>$url,'filename'=>$filename]]);
        } else {
        	return json(['error'=>0,'msg'=>$this->lang['UPLOAD'].$this->lang['FAIL']]);
         
        }
    }


    //修改昵称
	function upnickname(){
		$data= input('param.');
		if(empty($data['nickname'])){
			return json(['error'=>1,'msg'=>$this->lang['NICKNAME_INPUT_ALARM']]);
		}
		try {
			Db::name('user')->where('id',$this->user['id'])->update(['user_nickname'=>$data['nickname']]);
			return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS']]);
		} catch (Exception $e) {
			return json(['error'=>1,'msg'=>$this->lang['OPERATE'].$this->lang['FAIL']]);
		}

	}


	//我的团队
	function  mygroup(){
		$rows = Db::name('user')->where(['parent_user_id'=>$this->user['id'],'user_status'=>1])->order('id asc')->select()->toArray();
		$rows = $this->mul_deal_user($rows);
		$ret['child_1'] = $rows;
		$child_2 = empty($rows)? []: $this->get_childs($rows);
		$ret['child_2'] = $child_2;
		$ret['child_3'] =empty($child_2)? [] : $this->get_childs($child_2);
		return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS'],'data'=>$ret]);
	}



	function  invited(){
		$page = $data['page']<=1 ? 1: $data['page'];
		$pagesize= $data['pagesize']<=0? 10: $data['pagesize'];
		$start = ($page-1) * $pagesize;
		$rows = Db::name('user')
		         ->where(['parent_user_id'=>$this->user['id'],'user_status'=>1])
		         ->order('id asc')
		         ->limit($start,$pagesize)
		         ->select()
		         ->toArray();
		$rows = $this->mul_deal_user($rows);
		return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS'],'data'=>$rows]);
	}



	protected  function  get_childs($users=array()){
		$pids =[];
		foreach ($users as $key => $value) {
			$pids[]= $value['id'];
		}
		$rows = Db::name('user')->where(['parent_user_id'=>['in',$pids],'user_status'=>1])->order('parent_user_id asc ')->select()->toArray();
		$rows = $this->mul_deal_user($rows);
		return $rows;
	}















	




}